<?php

/**
 * 客户端调用接口
 */
class Mobile_ServiceController extends MM_Controller_Mobile_Action
{    
    /**
     * 客户端访问接口
     * 
     * 参数params，是一个数组，每个元素由eid和synctime两项构成，如$arrParams[]=array('eid'=>1,'synctime'=>xxx);
     */
    public function syncAction()
    {
    	if ( ($this->uid<1) 
    			|| (!isset($this->httpdata['data'])) || empty($this->httpdata['data']) ) {
    		$this->result = $this->returnResult(Mediad2_Model_Result::CODE_FAILURE,'Missing parameter: data');
    	} else {
    		//初始化
    		$jsonData = $this->httpdata['data']; 
    		//将RequestData转成DeviceData和BusinessData
    		$arrayData = Zend_Json::decode($jsonData);
    		$params = $arrayData['params']; //注：params包含了要同步的event和synctime
    		
    		//同步处理
	   		$this->result = $this->service->sync($params);
    	}
    	echo $this->encodeResult($this->result);
    }
    
    /**
     * 客户端访问接口
     *
     * 参数params，是一个数组，每个元素由eid和synctime两项构成，如$arrParams[]=array('eid'=>1,'synctime'=>xxx);
     */
    public function syncsingleeventAction()
    {
    	if ( ($this->uid<1) || ($this->eid<1) 
    			|| (!isset($this->httpdata['synctime'])) ) {
    		$this->result = $this->returnResult(Mediad2_Model_Result::CODE_FAILURE,'Missing parameter: eid/synctime');
    	} else {
    		//初始化
    		$synctime = $this->httpdata['synctime'];
    		$params = array(  //注：params包含了要同步的event和synctime
    			array(
    				'eid' => $this->eid,
    				'synctime' => $synctime,
    			),
    		); 
    
    		//同步处理
    		$this->result = $this->service->sync($params);
    	}
    	echo $this->encodeResult($this->result);
    }
    
    /**
     * get event info
     * 当用户扫描二维码时，获取活动的信息
     */
    public function followeventAction()
    {
    	if (($this->uid<1) || ($this->eid<1) ) {
    		$this->result = $this->returnResult(Mediad2_Model_Result::CODE_FAILURE,'Miss parameter: uid/eid');
    	} else {
    		$this->result = $this->service->followEvent($this->eid, $this->uid);
    	}
    	echo $this->encodeResult($this->result);
    }
    
    /**
     * join event
     */
    public function joineventAction()
    {
    	if (($this->uid<1) || ($this->eid<1) ) {
    		$this->result = $this->returnResult(Mediad2_Model_Result::CODE_FAILURE,'Miss parameter: uid/eid');
    	} else {
    		$this->result = $this->service->joinEvent($this->eid, $this->uid,$this->httpdata);
    	}
    	echo $this->encodeResult($this->result);
    }
    
    public function tAction()
    {
    	/* $mmUser = new Mediad2_Service_Admin_User();
    	$rst = $mmUser->findBy(sprintf("email = '%s'","0"));
    	if($rst->getCode()){
    		$moblieUser = $rst->getData();
    		$uid = $moblieUser->getUid();
    		if(isset($uid)){
    			echo "have";
    		}else{
    			echo "none";    			
    		}
    	}
    	
    	var_dump($moblieUser,$moblieUser->getUid()); */
    	
    	$serviceFiles = new Mediad2_Service_Admin_Files();
    	$uuu = new Mediad2_Service_Admin_User();
    	//查询用户信息
    	$result = $uuu->find(1);
    	if ($result->getCode() == $result::CODE_FAILURE) {
    		return $result;
    	}
    	
    	//获取用户信息
    	$user = $result->getData();
    	
    	//获取空间背景图片
    	$spaceimg_fid = $user->getSpaceimg();
    	$resultSpaceImg = $serviceFiles->find($spaceimg_fid);
    	var_dump($spaceimg_fid,$resultSpaceImg);
    }
    
    /**
     * quit event
     */
    public function quiteventAction()
    {
    	if (($this->uid<1) || ($this->eid<1) ) {
    		$this->result = $this->returnResult(Mediad2_Model_Result::CODE_FAILURE,'Miss parameter: uid/eid');
    	} else {
    		$this->result = $this->service->quitEvent($this->eid, $this->uid);
    	}
    	echo $this->encodeResult($this->result);
    }
    
    /**
     * upload event files
     */
    public function uploadeventfilesAction()
    {
    	if ( ($this->uid<1) || ($this->eid<1) ) {
    		$this->result = $this->returnResult(Mediad2_Model_Result::CODE_FAILURE,'Missing parameter: uid/eid');
    		//$this->helper->log(sprintf("uid:%s,eid:%s",$_REQUEST['uid'],$_REQUEST['eid']));
    		//$this->helper->log("mm msg:".$this->result->getMessage().":".$this->result->getData());
    	} else {
    		$desc = empty($this->httpdata['desc'])?'':$this->httpdata['desc'];
    		$this->result = $this->service->uploadEventFiles($this->eid, $this->esid, $this->uid, $desc);
    		//$this->helper->log("mm msg:".$this->result->getMessage().":".$this->result->getData());
    	}
    	
    	echo $this->encodeResult($this->result);
    }
    
    /**
     * get event files list
     */
    public function geteventfilesAction()
    {
    	if ( ($this->uid<1) || ($this->eid<1) 
    			|| (!isset($this->httpdata['do'])) || empty($this->httpdata['do']) 
    			|| !isset($this->httpdata['addtime']) ) {
    		$this->result = $this->returnResult(Mediad2_Model_Result::CODE_FAILURE,'Missing parameter: uid/eid/addtime');
    	} else {
    		$addtime= empty($this->httpdata['addtime'])?0:$this->httpdata['addtime'];
    		$esid= empty($this->httpdata['esid'])?0:$this->httpdata['esid'];
    		$do = $this->httpdata['do'];
    		$this->result = $this->service->getEventFiles($this->eid, $esid, $this->uid, $do, $addtime);
    	}
    	
    	echo $this->encodeResult($this->result);
    }
    
    /**
     * get files info
     */
	public function getfilesinfoAction()
	{
		if ( ($this->uid<1) || ($this->eid<1) || ($this->fid<1) ) {
			$this->result = $this->returnResult(Mediad2_Model_Result::CODE_FAILURE,'Missing parameter: uid/eid/fid');
		} else {
			$this->result = $this->service->getFilesInfo($this->eid, $this->fid);
		}
		
		echo $this->encodeResult($this->result);
	}
	
	/**
	 * update files desc
	 */
	public function updatefilesdescAction()
	{
		if ( ($this->uid<1) || ($this->fid<1)
			|| (!isset($this->httpdata['filedesc'])) || empty($this->httpdata['filedesc']) ) {
			$this->result = $this->returnResult(Mediad2_Model_Result::CODE_FAILURE,'Missing parameter: uid/fid/desc');
		} else {
			$filedesc= $this->httpdata['filedesc'];
			$this->result = $this->service->updateFilesDesc($this->fid, $this->uid, $filedesc);
		}
		
		echo $this->encodeResult($this->result);
	}
	
	/**
	 * delete files
	 */
	public function deletefilesAction()
	{
		if ( ($this->uid<1) || ($this->fid<1) ) {
			$this->result = $this->returnResult(Mediad2_Model_Result::CODE_FAILURE,'Missing parameter: uid/fid');
		} else {
			$this->result = $this->service->deleteFiles($this->fid, $this->uid);
		}
		
		echo $this->encodeResult($this->result);
	}
	
	/**
	 * get files comment
	 */
	public function getfilescommentAction()
	{
		if ( ($this->uid<1) || ($this->fid<1)
				|| !isset($this->httpdata['addtime']) ) {
			$this->result = $this->returnResult(Mediad2_Model_Result::CODE_FAILURE,'Missing parameter: uid/fid/addtime');
		} else {
			$addtime= empty($this->httpdata['addtime'])?0:$this->httpdata['addtime'];
			$this->result = $this->service->getFilesComment($this->fid, $addtime);
		}
		
		echo $this->encodeResult($this->result);
	}
	
	/**
	 * get files fav
	 */
	public function getfilesfavAction()
	{
		if ( ($this->uid<1) || ($this->fid<1)
				|| !isset($this->httpdata['addtime']) ) {
			$this->result = $this->returnResult(Mediad2_Model_Result::CODE_FAILURE,'Missing parameter: uid/fid/addtime');
		} else {
			$addtime= empty($this->httpdata['addtime'])?0:$this->httpdata['addtime'];
			$this->result = $this->service->getFilesFav($this->fid, $addtime);
		}
	
		echo $this->encodeResult($this->result);
	}
	
	/**
	 * favor files
	 */
	public function favorfilesAction()
	{
		if ( ($this->uid<1) || ($this->fid<1) || ($this->eid<1)
				|| (!isset($this->httpdata['action'])) || empty($this->httpdata['action']) ) {
			$this->result = $this->returnResult(Mediad2_Model_Result::CODE_FAILURE,'Missing parameter: uid/fid/addtime');
		} else {
			$action = $this->httpdata['action'];
			$this->result = $this->service->favorFiles($this->eid, $this->fid, $this->uid, $action);
		}
		
		echo $this->encodeResult($this->result);
	}
    
    /**
     * create/reply files comment
     */
    public function createfilescommentAction()
    {
    	if (($this->uid<1) || ($this->eid<1) || ($this->fid<1)
    			|| !isset($this->httpdata['type']) || empty($this->httpdata['type']) ) {
    		$this->result = $this->returnResult(Mediad2_Model_Result::CODE_FAILURE,'Missing parameter: uid/eid/fid');
    	} else {
    		
    		if ($this->httpdata['replyeucid']) {
    			$reply_eucid = $this->httpdata['replyeucid'];
    		} else {
    			$reply_eucid = 0;
    		}
    		if ($this->httpdata['type']) $type = $this->httpdata['type'];
    		switch ($type) {
    			case 'voice':
    				$commentType = 'voice';
    				$content = '';
    				break;
    			case 'text':
    			default:
    				$commentType = 'text';
    				if (!isset($this->httpdata['content']) || empty($this->httpdata['content'])) {
    					$this->result = $this->returnResult(Mediad2_Model_Result::CODE_FAILURE,'Missing parameter: content');
    				}
    				if ($this->httpdata['content']) $content = $this->httpdata['content'];
    				break;
    		}
    		$objecttype = 'event_files';
    		$objectid = $this->fid;
    		$this->result = $this->service->createComment($commentType, $this->eid, $this->uid, $objecttype, $objectid, $content, $reply_eucid);
    	}
    
    	echo $this->encodeResult($this->result);
    }
    
    /**
     * delete comment
     */
    public function delcommentAction()
    {
    	if (($this->uid<1)
    			|| !isset($this->httpdata['eucid']) || empty($this->httpdata['eucid']) ) {
    		$this->result = $this->returnResult(Mediad2_Model_Result::CODE_FAILURE,'Missing parameter: uid/eucid');
    	} else {
    		if ($this->httpdata['eucid']) $eucid = $this->httpdata['eucid'];
    		$this->result = $this->service->delComment($this->uid, $eucid);
    	}
    
    	echo $this->encodeResult($this->result);
    }
    
    /**
     * add hand to event schedule
     */
    public function addhandscheduleAction()
    {
    	if (($this->uid<1) || ($this->eid<1)
    			|| !isset($this->httpdata['objectid']) || empty($this->httpdata['objectid']) ) {
    		$this->result = $this->returnResult(Mediad2_Model_Result::CODE_FAILURE,'Missing parameter: uid/eid/objectid');
    	} else {
    		$objecttype = 'event_schedule';
    		$objectid	= $this->httpdata['objectid'];
    		$applausetype	= 'hand';
    		$number = empty($this->httpdata['number'])?1:$this->httpdata['number'];
    		$this->result = $this->service->addApplause($this->eid, $objecttype, $objectid, $applausetype, $number);
    	}
    	
    	echo $this->encodeResult($this->result);
    }
    
    /**
     * add hiss to event schedule
     */
    public function addhissscheduleAction()
    {
    	if (($this->uid<1) || ($this->eid<1)
    			|| !isset($this->httpdata['objectid']) || empty($this->httpdata['objectid']) ) {
    		$this->result = $this->returnResult(Mediad2_Model_Result::CODE_FAILURE,'Missing parameter: uid/eid/objectid');
    	} else {
    		$objecttype = 'event_schedule';
    		$objectid	= $this->httpdata['objectid'];
    		$applausetype	= 'hiss';
    		$number = empty($this->httpdata['number'])?1:$this->httpdata['number'];
    		$this->result = $this->service->addApplause($this->eid, $objecttype, $objectid, $applausetype, $number);
    	}
    
    	echo $this->encodeResult($this->result);
    }
    
    /**
     * add flower to event guests
     */
    public function addflowerguestsAction()
    {
    	if (($this->uid<1) || ($this->eid<1)
    			|| !isset($this->httpdata['objectid']) || empty($this->httpdata['objectid']) ) {
    		$this->result = $this->returnResult(Mediad2_Model_Result::CODE_FAILURE,'Missing parameter: uid/eid/objectid');
    	} else {
    		$objecttype = 'event_guests';
    		$objectid	= $this->httpdata['objectid'];
    		$applausetype	= 'flower';
    		$number = empty($this->httpdata['number'])?1:$this->httpdata['number'];
    		$this->result = $this->service->addApplause($this->eid, $objecttype, $objectid, $applausetype, $number);
    	}
    
    	echo $this->encodeResult($this->result);
    }
    
    /**
     * add flower to event guests
     */
    public function geteventidsAction()
    {
    	if ($this->uid<1){
    		$this->result = $this->returnResult(Mediad2_Model_Result::CODE_FAILURE,'Missing parameter: uid');
    	} else {
    		$this->result = $this->service->getEventIds($this->uid);
    	}    
    	echo $this->encodeResult($this->result);
    }
    
    public function geteventlistbycodeAction()
    {
    	$qrcode = @$this->httpdata['code'];
    	if ($this->uid<1 || !isset($qrcode) || empty($qrcode)){
    		$this->result = $this->returnResult(Mediad2_Model_Result::CODE_FAILURE,'Missing parameter: uid/code');
    	} else {
    		$this->result = $this->service->getEventByCode($qrcode);
    	}
    	echo $this->encodeResult($this->result);
    }
}
	
	

	